/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.campusinfoeducation.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.campusinfo.modules.campusinfo.entity.CampusinfoSchool;
import com.lczyfz.campusinfo.modules.campusinfo.service.CampusinfoSchoolService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.campusinfoeducation.entity.CampusinfoCollege;
import com.lczyfz.campusinfo.modules.campusinfoeducation.service.CampusinfoCollegeService;

import java.util.List;

/**
 * 学院管理Controller
 * @author yt
 * @version 2017-10-18
 */
@Controller
@RequestMapping(value = "${adminPath}/campusinfoeducation/campusinfoCollege")
public class CampusinfoCollegeController extends BaseController {

	@Autowired
	private CampusinfoCollegeService campusinfoCollegeService;

	@Autowired
	private CampusinfoSchoolService campusinfoSchoolService;
	
	@ModelAttribute
	public CampusinfoCollege get(@RequestParam(required=false) String id) {
		CampusinfoCollege entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = campusinfoCollegeService.get(id);
		}
		if (entity == null){
			entity = new CampusinfoCollege();
		}
		return entity;
	}
	
	@RequiresPermissions("campusinfoeducation:campusinfoCollege:view")
	@RequestMapping(value = {"list", ""})
	public String list(CampusinfoCollege campusinfoCollege, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CampusinfoCollege> page = campusinfoCollegeService.findPage(new Page<CampusinfoCollege>(request, response), campusinfoCollege);
		List<CampusinfoCollege> campusinfoColleges = campusinfoCollegeService.findList(campusinfoCollege);
		model.addAttribute("campusinfoColleges", campusinfoColleges);
		model.addAttribute("page", page);
		return "modules/campusinfoeducation/campusinfoCollegeList";
	}

	@RequiresPermissions("campusinfoeducation:campusinfoCollege:view")
	@RequestMapping(value = "form")
	public String form(CampusinfoCollege campusinfoCollege, Model model) {
		CampusinfoSchool campusinfoSchool = new CampusinfoSchool();
		List<CampusinfoSchool> campusinfoSchools = campusinfoSchoolService.findList(campusinfoSchool);
		model.addAttribute("campusinfoSchools", campusinfoSchools);
		model.addAttribute("campusinfoCollege", campusinfoCollege);
		return "modules/campusinfoeducation/campusinfoCollegeForm";
	}

	@RequiresPermissions("campusinfoeducation:campusinfoCollege:edit")
	@RequestMapping(value = "save")
	public String save(CampusinfoCollege campusinfoCollege, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, campusinfoCollege)){
			return form(campusinfoCollege, model);
		}
		campusinfoCollegeService.save(campusinfoCollege);
		addMessage(redirectAttributes, "保存学院信息成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfoeducation/campusinfoCollege/?repage";
	}
	
	@RequiresPermissions("campusinfoeducation:campusinfoCollege:edit")
	@RequestMapping(value = "delete")
	public String delete(CampusinfoCollege campusinfoCollege, RedirectAttributes redirectAttributes) {
		campusinfoCollegeService.delete(campusinfoCollege);
		addMessage(redirectAttributes, "删除学院信息成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfoeducation/campusinfoCollege/?repage";
	}

}